<?php
class product extends CI_Controller
{
	  
	protected $_templates;
	
	public function __construct()
	{
        parent::__construct();
		
		$this->load->model('product_model','product');
		//$this->load->model('news_model','news');
		$this->load->helper('MY_number');
		$this->load->helper('number');
		$this->load->helper('str');
		$this->load->helper('img');
		//$this->load->config('config_menu');
		//$this->load->config('config_ismainmenu');
		$this->pre_message = "";
		//$this->permit_library->check_permit();
		//$this->session->set_userdata(array('Url'=>uri_string()));           
	}
	
	function index()
	{
		$data['title'] = 'Thống kê';
		$this->_templates['page'] = 'product/static';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function listproduct()
	{
		$data['title'] = 'Danh sách sản phẩm';
		$cid = (int)$this->uri->segment(3);
		$data['listcat'] = $this->product->getMaincat();
		$data['add'] = 'product/addproduct';
		//$data['export'] = 'product/exportExcel/'.$cid ;
		
		$config['base_url'] = base_url().'product/listproduct/'.$cid;  
		$config['total_rows']   =  $this->product->getNumProduct($cid);
		$data['num'] = $config['total_rows'];
		$config['per_page']  =   30;
		$config['uri_segment'] = 4; 
		$ord = (int) $this->input->post('orderby') ;
		$this->pagination->initialize($config);
		$data['list'] = $this->product->getAllProduct($cid,$config['per_page'],$this->uri->segment('4'), $ord);
		$data['pagination']    = $this->pagination->create_links();           
		$this->_templates['page'] = 'product/pro/listproduct'; 
		$this->site->load($this->_templates['page'],$data);          
	}

	function addproduct()
	{
		$data['title'] = 'Thêm mới sản phẩm';
		$data['listcat'] = $this->product->check_parentid(0);
		$data['listimg'] = $this->product->get_img_by_session($this->session->userdata('session_id'));
		//$data['menufacture'] = $this->product->getAllMenufacture();
		
		$this->form_validation->set_rules('ProductName','Tên sản phẩm','trim|required');
		$this->form_validation->set_rules('Price','Giá','trim|required');
		
		
		if($this->form_validation->run())
		{
			if($this->product->saveProduct())
			{
				$this->session->set_flashdata('message','Lưu thành công');
				redirect('product/listproduct');
			}else{
				$this->pre_message ="Lưu không thành công";
			}
		}    
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'product/pro/addproduct';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function editproduct()
	{
		$ProductID = $this->uri->segment(3);
		$data['listmaincat'] = $this->product->check_parentid(0);
		$data['listimg'] = $this->product->get_img_by_product_id($ProductID);
		$rs = $this->product->get_info_product($ProductID);
		
		$data['title'] = 'Cập nhật sản phẩm '.$rs->ProductName;
		
		$this->form_validation->set_rules('ProductName','Tên sản phẩm','trim|required');
		$this->form_validation->set_rules('Price','Giá cty','trim|required');
		
		if($this->form_validation->run())
		{          
			if($this->product->saveProduct())
			{
				$this->session->set_flashdata('message','Lưu thành công');
				redirect('product/listproduct');
			}
			else
			{
				$this->pre_message ="Lưu không thành công";
			}    
		}
		
/*		$listcat = $this->product->get_info_cat($rs->CategoryID);
		$seg_cat = explode('-',$listcat);
		$data['cat1'] = $seg_cat[0];
		$data['cat2'] = $seg_cat[1];
		$data['cat3'] = $seg_cat[2];
		$data['listcat'] = $listcat;
*/		$data['rs'] = $rs;
		$data['ProductID'] = $ProductID;
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'product/pro/editproduct';
		$this->site->load($this->_templates['page'],$data);
	} 
	
	function delsproduct()
	{
		$page = (int)$this->input->post('page');
		$catid = (int)$this->input->post('catid');
	  
		if(isset($_POST['btn_submitHN']) && ($_POST['btn_submitHN']=='OrderHN'))
		{
			$pid = $this->input->post('ar_pid');
			$ar_pid = explode(',',$pid);
			$num_pid =  count($ar_pid);
			for($i=0;$i<$num_pid;$i++){
				$Ordering = (int)$this->input->post('OrderHN'.$ar_pid[$i]);
				if($Ordering!=0)
				{
					$data = array(
						'OrderHN' => $Ordering
					);
					$this->db->where('ProductID',$ar_pid[$i]);
					if($this->db->update('product',$data)){
						$this->session->set_flashdata('message','Sắp xếp Sản phẩm HN thành công');
					}else{
						$this->session->set_flashdata('message','Sắp xếp Sản phẩm HN không thành công');
					}
				}
			}
		}
	  if(isset($_POST['btn_submit']) && ($_POST['btn_submit']=='Lưu'))
	  {
		$pid = $this->input->post('ar_pid');
		$ar_pid = explode(',',$pid);
		$num_pid =  count($ar_pid);
		for($i=0;$i<$num_pid;$i++){
			$Ordering = (int)$this->input->post('Order'.$ar_pid[$i]);
			if($Ordering!=0)
			{
				$data = array(
					'Ordering' => $Ordering
				);
				$this->db->where('ProductID',$ar_pid[$i]);
				if($this->db->update('product',$data)){
					$this->session->set_flashdata('message','Sắp xếp thành công');
				}else{
					$this->session->set_flashdata('message','Sắp xếp không thành công');
				}
			}
		}
		
	  }else{
		if(!empty($_POST['ar_id']))
		{
	
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->product->delproduct($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}              
	  }
	  redirect('product/listproduct/'.$catid.'/'.$page); 
	
	}
	
	function delproduct()
	{
		$id = $this->uri->segment(3);
		$catid = $this->uri->segment(4);
		$page = $this->uri->segment(5);
		if($this->product->delproduct($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
		redirect('product/listproduct/'.$page);          
	}  
	

}
?>